<template>
  <section>
    <!--工具条-->

    <!-- <el-col :xs="6" :sm="6" :md="6" :lg="6" style="height:670px;width:20%;overflow-y :auto;overflow-x :auto">
      <el-tree highlight-current :data="treedata" :props="defaultProps" @node-click="handleNodeClick" :default-expand-all="true" :expand-on-click-node="true">
      </el-tree> -->
    <!-- </el-col> -->
    <div style="display:flex;justify-content:space-between;width:100%;height:100%">
    <div style="width:20%;position:relative;">
      <div  style="position:absolute;width:100%;height:100%;overflow-y:auto;overflow-x:auto">
         <el-col  style="overflow:hidden;width:auto" >
        <el-tree  highlight-current :data="treedata" :props="defaultProps" @node-click="handleNodeClick" :default-expand-all="true" :expand-on-click-node="true">
        </el-tree>
      </el-col>
      </div>
    </div>
   <div style="width:80%;">
    <el-col>
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters">
        <el-form-item>
          <el-input size="small" style="width:212px;" v-model.trim="filters.userCode" placeholder="请输入用户编码"></el-input>
        </el-form-item>
        <el-form-item>
          <el-input size="small" style="width:212px;" v-model.trim="filters.businessNo" placeholder="请输入业务号"></el-input>
        </el-form-item>
        <el-form-item>
          <el-input size="small" style="width:212px;" v-model.trim="filters.mobile" placeholder="请输入用户手机"></el-input>
        </el-form-item>
        <el-form-item>
          <el-tooltip class="item" effect="dark" content="提现时间" placement="top">
            <el-date-picker size="small" style="width:212px;" v-model.trim="filters.Time" type="daterange" align="left" placeholder="请选择提现时间范围">
            </el-date-picker>
          </el-tooltip>
        </el-form-item>
        <el-form-item>
          <el-select size="small" clearable style="width:212px;" v-model="filters.status" placeholder="请选择状态">
            <!-- <el-option label="全部状态"  value="-1"  ></el-option> -->
            <el-option label="成功" value="00"></el-option>
            <el-option label="申请" value="1"></el-option>
            <el-option label="处理中" value="2"></el-option>
            <el-option label="失败" value="9"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item style="float:right;">
          <el-button size="small" type="primary" icon="upload2" v-show="cashExport" @click="exportExcels">导出</el-button>
        </el-form-item>
        <el-form-item style="float:right;">
          <el-button size="small" type="primary" v-show="cashQuery" icon="search" v-on:click="getLogs(true)">查询</el-button>
        </el-form-item>
      </el-form>
    </el-col>
    <!--列表-->
    <el-table :data="Logs" stripe border :height="winHeight-188" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
      <el-table-column prop="id" align="center" label="ID" width="80" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="businessNo" align="center" label="业务号" width="120" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="userCode" align="center" label="客户编码" min-width="125">
      </el-table-column>
      <el-table-column prop="phoneNo" align="center" label="客户手机号" min-width="130" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="status" align="center" label="状态" width="120" :formatter="formatStatus">
      </el-table-column>
      <el-table-column prop="money" align="center" label="金额" min-width="100">
      </el-table-column>
      <el-table-column prop="score" align="center" label="积分" min-width="100">
      </el-table-column>
      <el-table-column prop="rate" align="center" label="兑换比例" min-width="120">
      </el-table-column>
      <el-table-column prop="createTime" align="center" label="提现时间" min-width="120" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="payee" align="center" label="收款人姓名" min-width="120" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="idcard" align="center" label="身份证号" min-width="120" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="currency" align="center" label="币种" min-width="100" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="payeeBank" align="center" label="收款银行" min-width="120" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="payeeAccount" align="center" label="收款银行账号" min-width="130" show-overflow-tooltip>
      </el-table-column>
      <el-table-column label="操作" fixed='right' width="100">
        <template scope="scope">
          <el-button size="small" type="warning" v-show="showEidtbtn(scope.$index, scope.row)" @click="reissue(scope.$index, scope.row)">提现</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 补发界面 -->
    <el-dialog title="补发" v-model="detailFormVisible" :close-on-click-modal="false">
      <el-form :model="restaurants" label-width="120px">
        <el-row :gutter="24">
          <el-col :xs="24" :sm="24" :md="14" :lg="20">
            <el-form-item label="用户银行:" prop="productCname">
              <el-select v-model="restaurants.selectdatas" clearable style="width:80%" placeholder="请选择">
                <el-option v-for="(item,index) in restaurants.resItems" :key="index" :label="item.fullName" :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="detailFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
      </div>
    </el-dialog>
    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <!-- <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button> -->

      <el-pagination style="float:right" @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="page" :page-sizes="[10, 20, 40, 80]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </el-col>
    </el-col>
    </div>
    </div>
  </section>
</template>

<script>
import util from '../../common/js/util'

import commons from '../../common/js/common'
//import NProgress from 'nprogress'
import { mapState, mapActions } from 'vuex'
import { cashLog, integralCashPatch, findBankListAll,treeNextChannelList,orderAuth } from '../../api/api';

export default {
  data() {
    return {
      filters: {
        Time: '',
        userCode: '',
        mobile: '',
        status: '',
        businessNo: ''
      },
      detailFormVisible: false,
      Logs: [],
      lists: [],
      exportTotal: 0,
      restaurants: {
        selectdatas: '',
        resItems: [],
      },
      treedata: [],
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      resBusinessNo: '',
      total: 0,
      page: 1,
      comCode:'',
      pageSize: 10,
      loading: '',
      // fullscreenLoading:false,
      listLoading: false,
      addLoading: false,
      addFormVisible: false,
      cashExport: false,
      cashQuery: false,

      sels: [],//列表选中列
      userid: '',
      addFormRules: {

      },
      IsShow: false
    }
  },
  computed: mapState([
    'winWidth',
    'winHeight'
  ]),
  methods: {
    cellStyle(row, rowIndex, columnIndex) {
    },
    showEidtbtn: function (index, row) {
      return row.status == 9 ? true : false;
    },
    formatCreateTime: function (row, column) {
      return commons.getDaytime(row.createTime, 1, false);
    },
    formatStatus: function (row, column) {
      return commons.payeeStatus(row.status);
    },
    handleCurrentChange(val) {
      this.page = val;
      this.getLogs(true);
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.getLogs(true);
    },
    //获取列表
    getLogs(bool) {
      let para = new FormData();
      para.append("pageSize", bool ? this.pageSize : this.exportTotal);
      para.append("pageNumber", bool ? this.page : 1);
      para.append("userCode", this.filters.userCode);
      // para.append("comCode", this.comCode);
      para.append("comCode", commons.isEmpty(this.comCode)?"00000000":this.comCode);
      para.append("mobile", this.filters.mobile);
      para.append("status", this.filters.status);
      para.append("businessNo", this.filters.businessNo);
      para.append("startDate", commons.changeTimes(this.filters.Time, 'start'));
      para.append("endDate", commons.changeTimes(this.filters.Time, 'end'));
      this.listLoading = bool;
      // this.fullscreenLoading = bool?false:true;
      if (!bool) {
        this.loading = this.$loading({
          lock: true,
          text: '正在导出',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        });
      }
      cashLog(para).then((res) => {
        this.lists = res.data.result.content;
        this.total = res.data.result.totalElements;
        this.exportTotal = res.data.result.totalElements;
        this.listLoading = false;
        if (!bool) {
          this.loading.close();
          this.export2Excel();
        }
        else {
          this.Logs = res.data.result.content;
        }
      });
    },
    handleNodeClick: function (data, node, obj) {
      this.comCode = data.id;
      let para = new FormData();
      para.append('branchNo', data.id);
      para.append('type', 'order');
      treeNextChannelList(para).then((res) => {
        if (res.data.result && res.data.success == true) {
          for (let i in res.data.result) {
            const newChild = { id: res.data.result[i].userCode, label: res.data.result[i].shopName, children: [] };
            if (!data.children) {
              this.$set(data, 'children', []);
            }
            if (typeof (commons.GetByID(this.treedata, res.data.result[i].userCode)) == 'undefined') {
              data.children.push(newChild);
            }
          }
        }
      });
      this.getLogs(true);
    },

    getSelectPerTree: function () {
      let para = new FormData();
      var userinfo = sessionStorage.getItem('user');
      if (userinfo) {
        var user = JSON.parse(userinfo);
      }
      var userid = user.userId;
      para.append('userId', userid);
      para.append('type', 'order');
      let modelTree = this.treedata;
      let leafTree = [];
      orderAuth(para).then((res) => {
        var data = res.data.result;
        if (data) {
          this.comCode=data[0].branchNo;
        }
        gettree(data, this.treedata);
        console.log(this.treedata)
      });
      function gettree(arr, trees) {
        for (var i in arr) {
          if (typeof (commons.GetByID(trees, arr[i].branchNo)) == 'undefined') {
            trees.push({
              id: arr[i].branchNo,
              label: arr[i].branchCname,
              children: []
            });
          }
          if (arr[i].nextBranch) {
            let childTree = commons.GetByID(trees, arr[i].branchNo);
            gettree(arr[i].nextBranch, childTree.children);
          }
        }
      }
       // setTimeout(() => {
        this.getLogs(true);
      // }, 1000);
    },
    reissue: function (index, row) {
      this.restaurants.selectdatas = '';
      let para = new FormData();
      this.resBusinessNo = row.businessNo;
      para.append("userCode", row.userCode);
      this.detailFormVisible = true;
      findBankListAll(para).then((res) => {
        this.restaurants.resItems = res.data.result;
      });
    },
    //table导出excel
    exportExcels: function () {
      this.getLogs(false);
    },
    export2Excel: function () {
      require.ensure([], () => {
        const { export_json_to_excel } = require('../../vendor/Export2Excel');
        const tHeader = ['ID', '业务号', '客户编码', '客户手机号', '状态', '金额', '积分', '兑换比例', '提现时间', '收款人姓名', '身份证号', '币种', '收款银行', '收款银行账号'];
        const filterVal = ['id', 'businessNo', 'userCode', 'phoneNo', 'status', 'money', 'score', 'rate', 'createTime', 'payee', 'idcard', 'currency', 'payeeBank', 'payeeAccount'];
        const list = this.lists;
        for (var i in list) {

          // list[i].createTime = commons.getDaytime(list[i].createTime, 1, false);
          list[i].status = commons.payeeStatus(list[i].status);
        }
        const data = this.formatJson(filterVal, list);
        export_json_to_excel(tHeader, data, '提现明细列表');
      })
    },
    formatJson: function (filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => v[j]))
    },
    selsChange: function (sels) {
      this.sels = sels;
    },
    addSubmit: function () {
      let para = new FormData();
      para.append('businessNo', this.resBusinessNo);
      para.append('bankId', this.restaurants.selectdatas);

      // this.$refs.addForm.validate((valid) => {
      // if (valid) {
      this.$confirm('确认提交吗？', '提示', {}).then(() => {
        this.addLoading = true;
        // this.addrequest(this.addForm);

        integralCashPatch(para).then((res) => {
          if (res.data.success) {
            this.$message({
              message: '提交成功',
              type: 'success'
            });
            this.detailFormVisible = false;
            this.getLogs(true);
          }
          else {
            this.$message({
              message: res.data.result,
              type: 'error'
            });
          }
        });
        // }
      });
    },
    btnShow: function () {
      let thisData = JSON.parse(sessionStorage.getItem('175'));
      let _this = this;
      $.each(thisData, function (i, j) {
        if (thisData[i] == "cashExport") {
          _this.cashExport = true;
        }
        if (thisData[i] == "cashQuery") {
          _this.cashQuery = true;
        }
      });
    },

  },
  mounted() {
    this.getSelectPerTree();
    // this.getLogs(true);
    this.btnShow();
  }
}

</script>

<style scoped>
  .el-tree {
    border: 0;
  }
</style>